// pages/bookEnter/bookList/index.js
const appData = getApp()
Component({
  /**
   * 组件的属性列表
   */
  lifetimes: {
    attached() {
      this.getbookListData() // 获取gender初始数据
    },
    // 在组件实例进入页面节点树时执行
    detached() {
      // 在组件实例被从页面节点树移除时执行
    }
  },
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    bookList: {}, // 书单的初始数据

    lists: [{
        title: "书单专区",
        content: ["男生专区", "女生专区"],
        styleBgcolor: ["#f0f0f0", ""]
      },
      {
        title: "热门",
        content: ["本周最热", "最新发布", "最多收藏"],
        styleBgcolor: ["#f0f0f0", "", ""]
      },
      {
        title: "标签分类",
        content: ["都市", "古代", "架空", "重生", "玄幻", "网游"],
        styleBgcolor: ["#f0f0f0", "", "", "", "", ""]
      }
    ],

  },

  /**
   * 组件的方法列表
   */
  methods: {
    getbookListData() {
      let ranking = appData.globalData.bookList;
      if (ranking.length > 0) {
        this.setData({
          bookList: ranking
        })

        // this.saveAppData()
      } else {
        setTimeout(() => {
          this.getbookListData()
        }, 300)
      }
    },


    submit(e) { // 更改书单数据 以及 样式更换
      let number = e.detail.target.id;
      number = number.split("-");

      let list = this.data.lists;
      let lists = list[parseInt(number[0])].styleBgcolor;
      lists = lists.map((val, index) => {
        if (index === parseInt(number[1])) {
          val = "#f0f0f0"
        } else {
          val = ""
        }
        return val
      })
      list[parseInt(number[0])].styleBgcolor = lists
      this.setData({
        lists: list
      })

      let cs1 = this.getCs(0);
      let cs2 = this.getCs(1);
      let cs3 = this.getCs(2);

      if (cs1 === "男生专区") {
        cs1 = "male"
      } else {
        cs1 = "female"
      }


      if (cs2 === "本周最热") {
        cs2 = ""
      } else if (cs2 === "最新发布") {
        cs2 = "created"
      } else {
        cs2 = "collectorCount"
      }

      this.getbookList(cs1, cs2, cs3);
    },

    getCs(num) {
      let data = 0;

      this.data.lists[num].styleBgcolor.forEach((val, index) => {
        if (val !== "") {
          data = index
        }
      })

      data = this.data.lists[num].content[data];

      return data;
    },

    getbookList(cs1, cs2, cs3) {
      this.setData({
        bookList: ""
      })
      wx.request({
        url: `https://api.zhuishushenqi.com/book-list?gender=${cs1}&sort=${cs2}&tag=${cs3}`,
        success: res => {

          let resData = res.data.bookLists;
          resData.forEach(val => {
            if (val.gender === "male") {
              val.gender = "男生专区"
            } else {
              val.gender = "女生专区"
            }

            let http = val.cover.indexOf("http");
            http = val.cover.slice(http);
            val.cover = decodeURIComponent(http);

            for (let i = 0; i < val.covers.length; i++) {
              let ehttp = val.covers[i].indexOf("http");
              val.covers[i] = decodeURIComponent(val.covers[i].slice(ehttp))
            }

          })
          // appData.setbookList(res.data.bookLists)
          this.setData({
            bookList: res.data.bookLists
          })
        }
      })
    },

    getInitData(par) { //父组件控制子组件的方案
      this.setData({
        bookList: par
      })
    },

    toListpage(e) {
      let id = e.currentTarget.id;
      wx.navigateTo({
        url: `/pages/bookEnter/bookList/listpage/index?id=${id}`
      })
    }
  }
})